package it.fantacalcio.hibernate.dao;

import it.fantacalcio.model.Partita;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;

public class PartitaDAOImpl extends BaseHibernateDAO implements PartitaDAO {

	@Override
	public void save(Partita partita) throws Exception {
		getSession().save(partita);
	}
	
	@Override
	public void delete(Partita partita) throws Exception {
		getSession().delete(partita);
	}
	
	@Override
	public void update(Partita partita) throws Exception {
		getSession().saveOrUpdate(partita);
	}

	@Override
	public Partita get(Long id) throws Exception {
		return (Partita) getSession().get(Partita.class, id);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Partita> list() throws Exception {
		Criteria cri = getSession().createCriteria(Partita.class);
		cri.addOrder(Order.asc("data"));
		List<Partita> results = cri.list();
		return results;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Partita> findByExample(Partita partita) throws Exception {
		Criteria cri = getSession().createCriteria(Partita.class);
		cri.add(Example.create(partita).ignoreCase());
		List<Partita> results = cri.list();
		return results;
	}

}
